

cc.Class({
    extends: cc.Component,

    properties: {
    
        ironBar : {             //铁矿progressBar
            default : null,
            type : cc.ProgressBar,
        },
        ironCount : {           //铁矿数量
            default : null,
            type : cc.Label,
        },
        stoneBar : {             //石头progressBar
            default : null,
            type : cc.ProgressBar,
        },
        stoneCount : {           //石头数量
            default : null,
            type : cc.Label,
        },
        goldBar :{
            default:null,
            type :cc.ProgressBar
        },
        goldCount :{
            default:null,
            type : cc.Label
        },
        diamondBar :{
            default:null,
            type :cc.ProgressBar
        },
        diamondCount :{
            default:null,
            type : cc.Label
        },
        silverCoinCount : {           
            default : null,
            type : cc.Label,
        },
        goldCoinCount : {             
            default : null,
            type : cc.Label,
        },
        
    },


    onLoad () {
        var self = this
    
        var params = {
            userid :cc.vv.userMgr.userid
        }
        cc.log(params)
        cc.vv.http.sendRequest("getResourceInfo",params,function(data) {
            
            if (data.code == 400) {
                cc.log("errmsg",data.msg)
            }
            
            self.initResource(data.data.resourceInfo)
        
            self.setResource()
        },cc.vv.url.resourceServer)
        self.updateInit()
    },
    setResource() {
        var res = cc.vv.resourceMgr
        
        if(res.stone.nowCount > 1) {
            
            this.stoneCount.string = this.strToN(res.stone.nowCount) + "万"
        }else {
            this.stoneCount.string = this.strToN(res.stone.nowCount)* 10000
        }
        this.stoneBar.progress = res.stone.nowCount / res.stone.maxCount

        if(res.iron.nowCount > 1) {
            this.ironCount.string = this.strToN(res.iron.nowCount) + "万"
        }else {
            this.ironCount.string = this.strToN(res.iron.nowCount) * 10000
        }
        this.ironBar.progress = res.iron.nowCount / res.iron.maxCount

        if(res.gold.nowCount > 1) {
            this.goldCount.string = this.strToN(res.gold.nowCount)+ "万"
        }else {
            this.goldCount.string = this.strToN(res.gold.nowCount) * 10000
        }
        this.goldBar.progress = res.gold.nowCount/res.gold.maxCount

        if(res.diamond.nowCount > 1) {
            this.diamondCount.string = this.strToN(res.diamond.nowCount )+ "万"
        }else {
            this.diamondCount.string = this.strToN(res.diamond.nowCount ) * 10000
        }
        this.diamondBar.progress = res.diamond.nowCount  / res.diamond.maxCount


        this.silverCoinCount.string = res.silverCoin.nowCount

        this.goldCoinCount.string = res.goldCoin.nowCount
        

    },
    initResource(resourceInfo) {
       cc.log("拿到的资源信息：",resourceInfo)
        for(var  i=0;i<resourceInfo.length;i++)  {
            
            var nowCount = this.strToN(resourceInfo[i].count);
            var maxCount = this.strToN(resourceInfo[i].max_count);
            var addRate = this.strToN(resourceInfo[i].add_rate);
            cc.log("nowCount:",nowCount)
            switch(resourceInfo[i].type){
                case "diamond":
                    cc.vv.resourceMgr.diamond = {};
                    cc.vv.resourceMgr.diamond.nowCount = nowCount;
                    
                    cc.vv.resourceMgr.diamond.maxCount = maxCount;
                    cc.vv.resourceMgr.diamond.addRate = addRate;
                    break;
                case "gold":
                    cc.vv.resourceMgr.gold = {};
                    cc.vv.resourceMgr.gold.nowCount = nowCount;
                    cc.vv.resourceMgr.gold.maxCount = maxCount;
                    cc.vv.resourceMgr.gold.addRate = addRate;
                    break;
                case "stone":
                    cc.vv.resourceMgr.stone = {};
                    cc.vv.resourceMgr.stone.nowCount = nowCount;
                    cc.vv.resourceMgr.stone.maxCount = maxCount;
                    cc.vv.resourceMgr.stone.addRate = addRate;
                    break;
                case "iron":
                    cc.vv.resourceMgr.iron = {};
                    cc.vv.resourceMgr.iron.nowCount = nowCount;
                    cc.vv.resourceMgr.iron.maxCount = maxCount;
                    cc.vv.resourceMgr.iron.addRate = addRate;
                    break;
                case "silverCoin":
                    cc.vv.resourceMgr.silverCoin = {};
                    cc.vv.resourceMgr.silverCoin.nowCount = nowCount;
                    cc.vv.resourceMgr.silverCoin.maxCount = maxCount;
                    cc.vv.resourceMgr.silverCoin.addRate = addRate;
                    break;
                case "goldCoin":
                    cc.vv.resourceMgr.goldCoin = {};
                    cc.vv.resourceMgr.goldCoin.nowCount = nowCount;
                    cc.vv.resourceMgr.goldCoin.maxCount = maxCount;
                    cc.vv.resourceMgr.goldCoin.addRate = addRate;
                    break;
                
                default : 
                    cc.log("initResources switch is default");
            }
        }
    },
   
    
    
     strToN(str) {
        var num = parseFloat(str);
        return Math.floor(num * 10000) / 10000;
    },
    
    updateInit(){
        this.upTime = cc.vv.config.resourceUpdateTime;
        
        this.gTime = 0;
    },
    updateResources(time){ //更新资源当前数量
        var t = time/3600;
        var res = cc.vv.resourceMgr;
        
        var diamondNowCount = this.strToN(res.diamond.nowCount + t * res.diamond.addRate);
        
        if(diamondNowCount <= res.diamond.maxCount){
            res.diamond.nowCount = diamondNowCount;
        }else {
            res.diamond.nowCount = res.diamond.maxCount
        }
        
        var goldNowCount = this.strToN(res.gold.nowCount + t * res.gold.addRate);
        if(goldNowCount <= res.gold.maxCount){
            res.gold.nowCount = goldNowCount;
        }else {
            res.gold.nowCount = res.gold.maxCount
        }

        var stoneNowCount = this.strToN(res.stone.nowCount + t * res.stone.addRate);
        
        if(stoneNowCount <= res.stone.maxCount){
            res.stone.nowCount = stoneNowCount;
        }else  {
            res.stone.nowCount = res.stone.maxCount
        }

        var ironNowCount = this.strToN(res.iron.nowCount + t * res.iron.addRate);
        if(ironNowCount <= res.iron.maxCount){
            res.iron.nowCount = ironNowCount;
        }else {
            res.iron.nowCount = res.iron.maxCount
        }

        
    },
    start () {

    },

    update (dt) {
        this.gTime += dt;
        if(this.gTime>this.upTime){ //每过一段时间更新资源
            
            //更新资源
            this.updateResources(this.gTime);
            this.setResource();
            this.gTime = 0;

            this.sendResourceUpdateToServer()
        }
    },
    sendResourceUpdateToServer() {
        var res = cc.vv.resourceMgr;
        var params = {
            userid: cc.vv.userMgr.userid,
            
            diamond: res.diamond.nowCount,
            gold: res.gold.nowCount,
            stone: res.stone.nowCount,
            iron: res.iron.nowCount,
            silverCoin: res.silverCoin.nowCount,
            goldCoin: res.goldCoin.nowCount
            
        };
        
        cc.vv.http.sendRequest("updateResourceInfo", params, function(data) {
            if (data.code !== 200) {
                cc.log("更新资源失败！:", data.msg);
            } 
                
        }, cc.vv.url.resourceServer);
    }
});
